package com.imis.module.system.bus;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imis.base.constant.DictCodeConstant;
import com.imis.base.constant.enums.SysTipEnum;
import com.imis.base.globle.Result;
import com.imis.base.util.CurrentUserUtils;
import com.imis.module.base.BaseBus;
import com.imis.module.system.model.converter.SysWebsocketMessageConverter;
import com.imis.module.system.model.po.SysRichText;
import com.imis.module.system.model.po.SysWebsocketMessage;
import com.imis.module.system.model.ro.PagingQueryWebsocketMessageDTO;
import com.imis.module.system.model.ro.SysWebsocketMessageRO;
import com.imis.module.system.model.vo.SysWebsocketMessageInfoVO;
import com.imis.module.system.model.vo.SysWebsocketMessageVO;
import com.imis.module.system.service.ISysDictService;
import com.imis.module.system.service.ISysRichTextService;
import com.imis.module.system.service.ISysWebsocketMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * WebSocket信息传递记录 业务处理类
 * </p>
 *
 * @author XinLau
 * @since 2020-07-24
 */
@Slf4j
@Service
public class SysWebsocketMessageBus extends BaseBus {

    /**
     * WebSocket信息传递记录 服务类
     */
    private ISysWebsocketMessageService sysWebsocketMessageService;

    @Autowired
    public void setISysWebsocketMessageService(ISysWebsocketMessageService sysWebsocketMessageService) {
        this.sysWebsocketMessageService = sysWebsocketMessageService;
    }

    /**
     * 字典项 服务类
     */
    private ISysDictService sysDictService;

    @Autowired
    public void setSysDictService(ISysDictService sysDictService) {
        this.sysDictService = sysDictService;
    }

    /**
     * 富文本 服务类
     */
    private ISysRichTextService sysRichTextService;

    @Autowired
    public void setSysRichTextService(ISysRichTextService sysRichTextService) {
        this.sysRichTextService = sysRichTextService;
    }

    /**
     * 批量保存WebsocketMessages记录
     *
     * @param sysWebsocketMessageROArrayList - WebSocket信息传递记录
     * @return Boolean -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/24 17:32
     */
    private Boolean batchSaveWebsocketMessages(final List<SysWebsocketMessageRO> sysWebsocketMessageROArrayList) {
        // 信息文本
        String content = sysWebsocketMessageROArrayList.get(0).getContent();
        SysRichText sysRichText = new SysRichText();
        sysRichText.setContent(content);
        // sysRichText.setTextType();
        // sysRichText.setPrimaryTableId();
        sysRichTextService.save(sysRichText);
        List<SysWebsocketMessage> sysWebsocketMessageList = SysWebsocketMessageConverter.INSTANCE.listRo2ListPo(sysWebsocketMessageROArrayList);
        sysWebsocketMessageList.forEach(sysWebsocketMessage -> {
            sysWebsocketMessage.setTextId(sysRichText.getId());
            sysWebsocketMessage.setId(IdWorker.getId());
            sysWebsocketMessage.setCreateBy(CurrentUserUtils.getLoginUserId());
            sysWebsocketMessage.setCreateTime(LocalDateTime.now());
        });
        return sysWebsocketMessageService.saveBatchSysWebsocketMessage(sysWebsocketMessageList);
    }

    /**
     * 分页查询WebSocket信息接口
     *
     * @param pagingQueryWebsocketMessageDTO - WebSocket信息传递记录查询对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/24 14:39
     */
    public Result pagingQuerySysWebsocketListByParameter(final PagingQueryWebsocketMessageDTO pagingQueryWebsocketMessageDTO) {
        try {
            Page<SysWebsocketMessageVO> sysWebsocketMessageListByParameter = sysWebsocketMessageService.pagingQuerySysWebsocketListByParameter(pagingQueryWebsocketMessageDTO);
            return Result.ok(sysWebsocketMessageListByParameter);
        } catch (Exception e) {
            return Result.errorsFormat(SysTipEnum.WEBSOCKET_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * WebSocket信息查看接口
     *
     * @param websocketIdentification - WebSocket信息传递记录标识
     * @return null -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/24 14:48
     */
    public Result queryWebsocketById(final Long websocketIdentification) {
        SysWebsocketMessageInfoVO sysWebsocketMessage = sysWebsocketMessageService.queryWebsocketById(websocketIdentification);
        if (sysWebsocketMessage == null) {
            return Result.errorsFormat(SysTipEnum.WEBSOCKET_QUERY_ERR);
        }
        String messageStateText = sysDictService.queryDictTextByKey(DictCodeConstant.MESSAGE_STATE, sysWebsocketMessage.getMessageState().toString());
        sysWebsocketMessage.setMessageStateText(messageStateText);
        return Result.ok(sysWebsocketMessage);
    }

    /**
     * 批量保存WebsocketMessages记录
     *
     * @param sysWebsocketMessageROArrayList - WebsocketMessages记录数组
     * @return Boolean -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/7/24 17:24
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchAddWebsocketMessages(final List<SysWebsocketMessageRO> sysWebsocketMessageROArrayList) {
        return this.batchSaveWebsocketMessages(sysWebsocketMessageROArrayList);
    }

}